<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- //! 绘制头部区域 -->
        <treetools
          :tree-data="company"
          :is-root="true"
          @openDialog="openDialogFn"
        />

        <!-- 树组件区域 -->
        <!-- data:树组件的数据 -->
        <!-- props:展示规则属性(值是一个对象{label：'文字显示的字段默认为label',children:'子级存在的字段，默认为children'}) -->
        <el-tree :data="data" :props="{ label: 'name', children: 'children' }">
          <!-- //! 再次复用头部组件 -->
          <treetools
            slot-scope="{ data }"
            :tree-data="data"
            @updataList="initData"
            @openDialog="openDialogFn"
            @editDialog="editDialogFn"
          />
        </el-tree>
      </el-card>
    </div>
    <!-- //!新增和修改的弹框 -->
    <add-dept ref="addDept" @updataList="initData" />
  </div>
</template>

<script>
import AddDept from './components/add-dept.vue'
import { tranListToTreeData } from '@/utils'
import { getDepartmentListApi } from '@/api/department'
import treetools from './components/tree-tools.vue'
export default {
  components: {
    treetools,
    AddDept
  },
  data() {
    return {
      // 1.定义变量，存储数据
      data: [],
      company: { name: '传智教育', manager: '负责人', id: '' } // !顶级边缘处理id:''
    }
  },
  created() {
    // 3.生命周期调用
    this.initData()
  },
  methods: {
    // 2.获取数据
    async initData() {
      // 2.1发送请求
      const { companyName, depts } = await getDepartmentListApi()
      // 2.2赋值数据
      this.company.name = companyName
      this.data = tranListToTreeData(depts, '')
    },
    openDialogFn(val) {
      // 通过ref来获取到组件实例对象，直接修改里面的数据
      console.log(this.$refs.addDept)
      console.log(val)
      this.$refs.addDept.dialogShow = true
      this.$refs.addDept.formData.pid = val
    },
    editDialogFn(val) {
      console.log('我要修改id为' + val + '的数据')
      this.$refs.addDept.getFormDataInfo(val)
    }
  }
}
</script>

<style scoped>
.tree-card {
  padding: 30px 140px;
  font-size: 14px;
}
</style>
